// pages/video/video.js
//视频接口：https://wxapi.wxappclub.com/news/video
//Method: GET
//参数：page(default=1)
//result:
// [
//   {
//     "id": "1",
//     "title": "新闻联播评反华势力给中国泼脏水",
//     "pubTime": "17:58",
//     "url": "http://assets.wxappclub.com/lesson/news/news_video_1.mp4",
//     "poster": "http://assets.wxappclub.com/lesson/news/news_video_1.mp4?vframe/jpg/offset/1"
//   },
//   {
//     "id": "2",
//     "title": "美多地上演反对“居家令”示威 特朗普：上街的都是很负责任的",
//     "pubTime": "17:56",
//     "url": "http://assets.wxappclub.com/lesson/news/news_video_2.mp4",
//     "poster": "http://assets.wxappclub.com/lesson/news/news_video_2.mp4?vframe/jpg/offset/1"
//   }
// ]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNo: 0,
    windowHeight: 0,
    currentPlay: null,
    isLoading: false,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getWindowsHeight();
    //读取接口数据
    this.getVideoList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    //读取接口数据
    this.getVideoList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  //获取系统高度
  getWindowsHeight() {
    let that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      },
    })
  },

  //获取列表高度
  getListHeight() {
    let that = this;
    const query = wx.createSelectorQuery();
    query.select('.container').boundingClientRect();
    query.selectViewport();
    query.exec(function(res) {
      if (res[0].height < that.data.windowHeight) {
        //读取接口数据
        that.getVideoList();
      }
    })
  },

  //加载快讯列表
  getVideoList() {
    if (this.data.hasMore) {
      //页码+1
      this.setData({
        isLoading: true,
        pageNo: this.data.pageNo + 1
      });
      let that = this;
      wx.request({
        url: 'https://wxapi.wxappclub.com/news/video',
        data: {
          page: that.data.pageNo
        },
        success: function(res) {
          that.setData({
            isLoading: false
          });
          if (res.data.length > 0) {
            that.setData({
              list: that.data.list.concat(res.data)
            })
            that.getListHeight();
          } else {
            that.setData({
              hasMore: false,
              pageNo: that.data.pageNo - 1
            });
            wx.showToast({
              title: '没有更多了',
              icon: 'none',
              duration: 2000
            })
          }

        }
      })
    }

  },

  videoPlay(e) {
    let dataId = e.currentTarget.dataset.id;
    let videoContext = wx.createVideoContext("video_" + dataId); //video_1
    if (this.data.currentPlay != null) {
      this.data.currentPlay.pause();
    }
    this.setData({
      currentPlay: videoContext
    });
  }


})